//
//  changePsdViewController.swift
//  NewLndd
//
//  Created by 邵禹曦 on 2020/1/9.
//  Copyright © 2020 辽宁当代. All rights reserved.
//

import UIKit
// MARK: - 修改密码
class changePsdViewController: ApiViewController {
var _comInfo : String?
    @IBOutlet weak var oldPsd: UITextField!
    @IBOutlet weak var newPsd: UITextField!
    @IBOutlet weak var newPsd1: UITextField!
    @IBOutlet weak var tj: UIButton!
    @IBOutlet weak var cz: UIButton!
    
    var oldtext : String?
    var newtext : String?
    override func viewDidLoad() {
        super.viewDidLoad()
        showInfo()
    }
    
}


extension changePsdViewController {
    private func showInfo() {
           // GCD
           let group = DispatchGroup()
    
           // 从沙盒中获取参数
            var userPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
            userPath =  (userPath as NSString).appendingPathComponent("data.plist")
            user = NSKeyedUnarchiver.unarchiveObject(withFile: userPath) as? User


           // 用户验证
           self.u = user?.U!
           self.retcode = user?.RETCODE!
           // 用户验证
           let str = [u,"101",retcode]
            
           //self.isLogin = self._network(str: str as! [String], group: group)
        group.enter()
        NetworkTools.share.check_user(code: str as Array<Any>) { (result, error) in
                      
                          if error != nil {
                              print("no no no")
                              return
                          }
                          

                          if Int(self._retcode(res: result!))! < 0 {
                              self.isLogin = false
                          } else {
                              self.isLogin = true
                          }
                          group.leave()
                      }

           group.notify(queue: DispatchQueue.main) {
               if !self.isLogin {
                let alert = UIAlertController(title: "提示！", message: "请登陆！", preferredStyle: UIAlertController.Style.alert)
                let btnOK = UIAlertAction(title:"好的", style: .default) { (cancelAction)in
                    self.setupVisitorView()
                }
                alert.addAction(btnOK)
                self.present(alert, animated: true, completion: nil)
                return
               } else {
                   // 2.设置导航栏内容
                    self.setupView()
               }
            }
       }
}

//change_password
extension changePsdViewController {
    
    @objc func setupView() {
        
        tj.addTarget(self, action: #selector(psdBtnClick), for: .touchUpInside)
        cz.addTarget(self, action: #selector(backBtnClick), for: .touchUpInside)
    }
    

}

/*
 NetworkTools.share.changeEncode(str: oldPsd.text!) { (result, error) in
     self.oldPsd.text =  result!["data"] as? String
     g.leave()
 }
 
 NetworkTools.share.changeEncode(str: newPsd.text!) { (result, error) in
      self.newPsd.text =  result!["data"] as? String
      g.leave()
 }
 */

//MARK: - 绑定监听事件
extension changePsdViewController {
    
    // 绑定登陆按钮
    @objc private func psdBtnClick()
    {
        //loadPsd()
        if !_iVerify() {
            return
        }
        _enCodeNet()

    }
    
    // 返回上一页
    @objc private func backBtnClick()
    {
        oldPsd.text = ""
        newPsd.text = ""
        newPsd1.text = ""
    }
}

extension changePsdViewController {
    
    @objc private func _iVerify() -> Bool {
        if self.oldPsd.text == ""  {
         if #available(iOS 13.0, *) {
             self.showMsgbox(_message: "请输入旧密码")
         }
            return false
        }
        
        if self.newPsd.text == ""   {
         if #available(iOS 13.0, *) {
             self.showMsgbox(_message: "请输入新密码")
         }
            return false
        }
        
        if self.newPsd1.text == ""   {
         if #available(iOS 13.0, *) {
             self.showMsgbox(_message: "请重新输入新密码")
         }
            return false
        }
        
        if self.newPsd1.text != self.newPsd.text  {
         if #available(iOS 13.0, *) {
             self.showMsgbox(_message: "两次密码输入不一致")
         }
            return false
        }
        
        return true
    }
    
    
    @objc private func _enCodeNet(){
        let group = DispatchGroup()
        group.enter()
        
        //let queue = DispatchQueue(label: "network" ,qos: .utility, attributes:  .concurrent)
       
        NetworkTools.share.changeEncode(str: self.oldPsd.text!) { (result, error) in
            self.oldtext = result!["data"] as? String
            group.leave()
        }
        
        group.enter()
        NetworkTools.share.changeEncode(str: self.newPsd.text!) { (result, error) in
            self.newtext = result!["data"] as? String
            group.leave()
        }
        group.notify(queue: DispatchQueue.main) {
            self.loadPsd(oldp : self.oldtext!, new : self.newtext!)
        }
    }

    
}


// MARK: - 执行  _deCode
extension changePsdViewController {
    
    @objc private func  loadPsd(oldp : String, new : String) {
        
        // 绑定标签
            //let oldp = self.oldPsd.text
            //let new = self.newPsd.text
        //let new1 = newPsd1.text
            let str = [self.user?.U!,self.user?.RETCODE! ,oldp ,new]
        print(str)
        NetworkTools.share.change_password(code: str as Array<Any>) { (result, error) in
               
               // 错误校验
               if error != nil
               {
                if #available(iOS 13.0, *) {
                    self.showMsgbox(_message: "网络连接错误")
                }
                   print(error!)
                   return
               }
               
               //["message": 调用三方接口失败, "error": 100003]
               //self.showMsgbox(_message: result?["message"] as! String)
               //print(result!)
               
               // 请求成功跳转到登陆
               

               
               // 获取可选类型中的数组
               guard let resultArray = result else {
                   print("no")
                   return
               }
                print(resultArray)
               
               if let REP = resultArray["REP"] as? [String : AnyObject] {
                   if let arr = REP["RESULT"] as? [String : AnyObject]   {
                       let code : Int = Int(arr["RETCODE"] as! String)!
                       if(Int(truncating: NSNumber(value: code)) >= 0) {
                            if #available(iOS 13.0, *) {
                                self.showMsgbox(_message: "请求成功")
                            }
                       } else {
                            if #available(iOS 13.0, *) {
                                self.showMsgbox(_message: "请求失败")
                            }
                       }
                   }
               }
               
           }
       }
    
}
